package com.xiubai.appforpda.jack.utils;

import android.content.Context;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtils {
	/**
	 * 获取yyyyMMdd格式日期
	 * 
	 * @param time
	 * @return
	 */
	public static Date getDate(String time) {
		Date date = new Date(System.currentTimeMillis());
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		try {
			date = format.parse(time);
		} catch (Exception e) {
			e.printStackTrace();
			e.printStackTrace();
			try {
				date = format.parse("19901111");
			} catch (ParseException e1) {
				e1.printStackTrace();
			}
		}
		return date;
	}

	public static Date getDate2(String time) {
		Date date = new Date(System.currentTimeMillis());
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try {
			date = format.parse(date.toString());
		} catch (Exception e) {
			e.printStackTrace();
			try {
				date = format.parse("1990-11-11");
			} catch (ParseException e1) {
				e1.printStackTrace();
			}
		}
		return date;
	}

    /**
     * 获取现在时间
     *
     * @return返回短时间格式 yyyy-MM-dd
     */
    public static Date getNowDateShort() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(currentTime);
        ParsePosition pos = new ParsePosition(8);
        Date currentTime_2 = formatter.parse(dateString, pos);
        return currentTime_2;
    }

    /**
     * 获取现在时间
     *
     * @return 返回短时间字符串格式yyyy-MM-dd
     */
    public static String getStringDateShort() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

	public static String formatDate(Context context, long date) {
		@SuppressWarnings("deprecation")
		int format_flags = android.text.format.DateUtils.FORMAT_NO_NOON_MIDNIGHT
				| android.text.format.DateUtils.FORMAT_ABBREV_ALL
				| android.text.format.DateUtils.FORMAT_CAP_AMPM
				| android.text.format.DateUtils.FORMAT_SHOW_DATE
				| android.text.format.DateUtils.FORMAT_SHOW_DATE
				| android.text.format.DateUtils.FORMAT_SHOW_TIME;
		return android.text.format.DateUtils.formatDateTime(context, date,
				format_flags);
	}
/////////////////////////////////////////////////////////////////////////////////////////////////
   //New
   public static String[] WEEK = new String[]{"天", "一", "二", "三", "四", "五", "六"};

   private static final long ONE_SECOND = 1000;
   private static final long ONE_MINUTE = ONE_SECOND * 60;
   private static final long ONE_HOUR = ONE_MINUTE * 60;
   private static final long ONE_DAY = ONE_HOUR * 24;

   /**
    * String 转换 Date
    *
    * @param str
    * @param format
    * @return
    */
   public static Date string2Date(String str, String format) {
      try {
         return new SimpleDateFormat(format).parse(str);
      } catch (ParseException e) {
         e.printStackTrace();
      }
      return new Date();
   }

   /**
    * Date（long） 转换 String
    *
    * @param time
    * @param format
    * @return
    */
   public static String date2String(long time, String format) {
      SimpleDateFormat sdf = new SimpleDateFormat(format);
      String s = sdf.format(time);
      return s;
   }

   /**
    * long 去除 时分秒
    * 时分秒全部为0
    *
    * @param date
    * @return
    */
   public static long getYearMonthDay(long date) {
      Calendar calendar = Calendar.getInstance();
      calendar.setTimeInMillis(date);
      calendar.set(Calendar.HOUR_OF_DAY, 0);
      calendar.set(Calendar.SECOND, 0);
      calendar.set(Calendar.MINUTE, 0);
      calendar.set(Calendar.MILLISECOND, 0);
      return calendar.getTimeInMillis();
   }

   /**
    * 获取目标时间和当前时间之间的差距
    *
    * @param date
    * @return
    */
   public static String getTimestampString(Date date) {
      Date curDate = new Date();
      long splitTime = curDate.getTime() - date.getTime();
      if (splitTime < (30 * ONE_DAY)) {
         if (splitTime < ONE_MINUTE) {
            return "刚刚";
         }
         if (splitTime < ONE_HOUR) {
            return String.format("%d分钟前", splitTime / ONE_MINUTE);
         }

         if (splitTime < ONE_DAY) {
            return String.format("%d小时前", splitTime / ONE_HOUR);
         }

         return String.format("%d天前", splitTime / ONE_DAY);
      }
      String result;
      result = "M月d日 HH:mm";
      return (new SimpleDateFormat(result, Locale.CHINA)).format(date);
   }

   /**
    * 24小时制 转换 12小时制
    *
    * @param time
    * @return
    */
   public static String time24To12(String time) {
      String str[] = time.split(":");
      int h = Integer.valueOf(str[0]);
      int m = Integer.valueOf(str[1]);
      String sx;
      if (h < 1) {
         h = 12;
         sx = "上午";
      } else if (h < 12) {
         sx = "上午";
      } else if (h < 13) {
         sx = "下午";
      } else {
         sx = "下午";
         h -= 12;
      }
      return String.format("%d:%02d%s", h, m, sx);
   }

   /**
    * Date 转换 HH
    *
    * @param date
    * @return
    */
   public static String date2HH(Date date) {
      return new SimpleDateFormat("HH").format(date);
   }

   /**
    * Date 转换 HH:mm:ss
    *
    * @param date
    * @return
    */
   public static String date2HHmm(Date date) {
      return new SimpleDateFormat("HH:mm").format(date);
   }

   /**
    * Date 转换 HH:mm:ss
    *
    * @param date
    * @return
    */
   public static String date2HHmmss(Date date) {
      return new SimpleDateFormat("HH:mm:ss").format(date);
   }

   /**
    * Date 转换 MM.dd
    *
    * @param date
    * @return
    */
   public static String date2MMdd(Date date) {
      return new SimpleDateFormat("MM.dd").format(date);
   }

   /**
    * Date 转换 yyyy.MM.dd
    *
    * @param date
    * @return
    */
   public static String date2yyyyMMdd(Date date) {
      return new SimpleDateFormat("yyyy.MM.dd").format(date);
   }

   /**
    * Date 转换 MM月dd日 星期
    *
    * @param date
    * @return
    */
   public static String date2MMddWeek(Date date) {
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(date);
      int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
      return new SimpleDateFormat("MM月dd日 星期").format(date) + WEEK[dayOfWeek - 1];
   }

   /**
    * Date 转换 yyyy年MM月dd日 星期
    *
    * @param date
    * @return
    */
   public static String date2yyyyMMddWeek(Date date) {
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(date);
      int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
      return new SimpleDateFormat("yyyy年MM月dd日 星期").format(date) + WEEK[dayOfWeek - 1];
   }

}
